package com.uchengwang.functions.http.manager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.DeleteObjectRequest;
import com.alibaba.sdk.android.oss.model.DeleteObjectResult;
import com.alibaba.sdk.android.oss.model.GetObjectRequest;
import com.alibaba.sdk.android.oss.model.GetObjectResult;
import com.alibaba.sdk.android.oss.model.ObjectMetadata;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.ucity.listener.SortRichListener;
import com.uchengwang.ui.self.sortricheditor.view.editor.SEditorData;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.PathUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/5/17.
 * 阿里云 oss上传
 */

public class OSSManager {
    //1头像  2享说 3U城内容,社团纳新,社团/校吧内的贴子内的图片;
    public static final int OSS_SHARE = 2;
    public static final int OSS_HEAD = 1;
    public static final int OSS_CITY = 3;


    private String BUCKET_HEAD = "ucw-user";//存储头像
    private String BUCKET_PUBLISH = "ucw-publish";//U城内容,社团纳新,社团/校吧内的贴子内的图片;
    private String BUCKET_SHARE = "ucw-share";//享说的图片
    private String endpoint = "oss-cn-shanghai.aliyuncs.com";
    private String KEY = "LTAINxpwUlXjkbJ5";
    private String SECRET = "LzsoLsDqBIRnhqfID8BGHBA08wdBHM";
    private OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(KEY, SECRET);
    private static OSSManager manager;
    private OSS oss;
    private boolean isErrorImage;//是否是ios不能识别的图片格式
    private static int index = 0;//标记

    private OSSManager (){
        if (oss == null){
            ClientConfiguration conf = new ClientConfiguration();
            conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
            conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
            conf.setMaxConcurrentRequest(5); // 最大并发请求书，默认5个
            conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次

            oss = new OSSClient(UChengApplication.getContext(), endpoint, credentialProvider, conf);
        }
    }

    public static OSSManager getOSSManager() {
        index = 0;
        if (manager == null){
            manager = new OSSManager();
        }
        return manager;
    }

    /**
     * 上传图片 异步 单张
     * @param progressCallback  带进度callback
     * @param completedCallback 上传完图片回调
     * @param type 1头像  2享说 3U城内容,社团纳新,社团/校吧内的贴子内的图片;
     * */
    private String objectKey;
    public OSSAsyncTask putAsyncSingleProgress(int type, String fileUrl, OSSProgressCallback<PutObjectRequest> progressCallback,
                                            OSSCompletedCallback<PutObjectRequest, PutObjectResult> completedCallback){
        if (TextUtils.isEmpty(fileUrl)) {
            // url为空就没必要上传了，这里做的是跳过它继续上传的逻辑。
            return null;
        }
        File file = new File(fileUrl);
        if (null == file || !file.exists()) {
            // 文件为空或不存在就没必要上传了，这里做的是跳过它继续上传的逻辑。
            return null;
        }
        objectKey = getObjectKey(type, file);

        Log.d("oss", "oss objectKey="+objectKey);

        // 构造上传请求
        PutObjectRequest put = null;

        switch (type){
            case 1://头像
                put = new PutObjectRequest(BUCKET_HEAD,objectKey, fileUrl);
                Log.d("ucw", "上传头像图片");
                break;
            case 2://享说
                put = new PutObjectRequest(BUCKET_SHARE,objectKey, fileUrl);
                Log.d("ucw", "上传享说图片");
                break;
            case 3://U城内容,社团纳新,社团/校吧内的贴子内的图片;
                put = new PutObjectRequest(BUCKET_PUBLISH,objectKey, fileUrl);
                Log.d("ucw", "上传普通图片");
                break;
        }

        // 异步上传时可以设置进度回调
        if (progressCallback!= null){
            put.setProgressCallback(progressCallback);
        }
        OSSAsyncTask task = oss.asyncPutObject(put, completedCallback);

    // task.cancel(); // 可以取消任务
    // task.waitUntilFinished(); // 可以等待直到任务完成
        return task;

    }

    /**
     * 上传图片 异步 单张 不带进度callback
     * @param completedCallback 上传完图片回调
     * @param type 1头像  2享说 3U城内容,社团纳新,社团/校吧内的贴子内的图片;
     * */
    public OSSAsyncTask putAsyncSingleNoProgress(int type,String fileUrl, OSSCompletedCallback<PutObjectRequest, PutObjectResult> completedCallback){
        return putAsyncSingleProgress(type, fileUrl, null, completedCallback);
    }


    private List<String> successUrl;
    private OSSProgressCallback<PutObjectRequest> progressCallback;//进度控制
    /**
     * 阿里云OSS上传（默认是异步多文件上传）
     * @param urls
     * @param type 1头像  2享说 3U城内容,社团纳新,社团/校吧内的贴子内的图片;
     */
    public OSSAsyncTask putAsyncFiles(final int type, final List<String> urls, final OSSListener listener) {
        index++;//自增，此处是预防图片地址重合
        if (successUrl == null){
            successUrl = new ArrayList<>();//存放上传成功返回的地址
        }else if (successUrl != null && index ==1){
            successUrl.clear();
        }
        if (null == urls || urls.size() == 0) {
            return null;
        }

        // 上传文件
        final String url = urls.get(0);
        if (TextUtils.isEmpty(url)) {
            urls.remove(0);
            // url为空就没必要上传了，这里做的是跳过它继续上传的逻辑。
            putAsyncFiles(type, urls, listener);
            return null;
        }
        File file = new File(url);
        if (null == file || !file.exists()) {
            urls.remove(0);
            // 文件为空或不存在就没必要上传了，这里做的是跳过它继续上传的逻辑。
            putAsyncFiles(type, urls, listener);
            return null;
        }

        if (progressCallback == null){
            progressCallback = new OSSProgressCallback<PutObjectRequest>() {
                @Override
                public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
                    //每一个文件的上传进度
                    listener.onProgress(currentSize, totalSize);
                }
            };
        }
        OSSAsyncTask task = putAsyncSingleProgress(type, url, progressCallback, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
              @Override
              public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                  // http://ucw-image.oss-cn-shanghai.aliyuncs.com/ucw_alioss_1495025209901.jpg
                  //多图上传 后台只要objectKey
//                  String imageurl = "http://"+BUCKET+"."+endpoint+"/"+objectKey;
                  if (isErrorImage){
                      objectKey = getObjectKeyToServer(type);
                      isErrorImage = false;
                  }
                  successUrl.add(objectKey);

                  // 上传成功
                  urls.remove(0);

                  if (urls.size() <= 0) {
                      // 文件全部上传完毕，这里编写上传结束的逻辑，如果要在主线程操作，最好用Handler或runOnUiThead做对应逻辑
                        listener.onSuccess(successUrl);
                      return;
                  }
                  putAsyncFiles(type, urls, listener);// 递归同步效果
              }

            @Override
            public void onFailure(PutObjectRequest request, ClientException client, ServiceException service) {
                // 上传失败
                // 请求异常
                if (client != null) {
                    // 本地异常如网络异常等
                    Log.e("jjf", client.getMessage());
                    client.printStackTrace();
                    listener.onFailure("本地异常");
                }
                if (service != null) {
                    // 服务异常
                    Log.e("ErrorCode", service.getErrorCode());
                    Log.e("RequestId", service.getRequestId());
                    Log.e("HostId", service.getHostId());
                    Log.e("RawMessage", service.getRawMessage());
                    listener.onFailure("errorCode="+service.getErrorCode()+"=rawMessage="+service.getRawMessage());
                }
            }
           });

            // task.cancel(); // 可以取消任务
           // task.waitUntilFinished(); // 可以等待直到任务完成
        return task;
        }


        /**
         * 专门针对图文交叉排布的多图片上传
         * @param pubType 1头像  2享说 3U城内容,社团纳新,社团/校吧内的贴子内的图片;
         * */

        public void putSortRichImages(final int pubType, final List<SEditorData> datas, final SortRichListener listener){
            String type = datas.get(index).getType();//0图片  1文字
            String path = datas.get(index).getContent();

            if (type.equals("1") || (type .equals("0") && TextUtils.isEmpty(path))){
                //本地图片地址为空
                if (type.equals("1")){
                    //转换成json时,将双引号转为单引号,不然解析出错
                    datas.get(index).setContent(UCommonUtil.getUtil().replaceQuotes(path));
                }
                index++;
                if (index >= datas.size()){
                    //说明已经上传结束
                    listener.onsuccess(datas);
                    return;
                }
                putSortRichImages(pubType, datas, listener);
                return;
            }

            File file = new File(path);
            if (null == file || !file.exists()) {
                // 文件为空或不存在就没必要上传了，这里做的是跳过它继续上传的逻辑。
                datas.remove(datas.get(index));
                if (index >= datas.size()){
                    //说明已经上传结束
                    listener.onsuccess(datas);
                    return;
                }
                putSortRichImages(pubType, datas, listener);
                return ;
            }

            if (progressCallback == null){
                progressCallback = new OSSProgressCallback<PutObjectRequest>() {
                    @Override
                    public void onProgress(PutObjectRequest putObjectRequest, long l, long l1) {
                        //每一个文件的上传进度
                    }
                };
            }

            OSSAsyncTask task = putAsyncSingleProgress(pubType, path, progressCallback, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
                @Override
                public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                    // http://ucw-image.oss-cn-shanghai.aliyuncs.com/ucw_alioss_1495025209901.jpg
                    // 上传成功
                    if (isErrorImage){
                        objectKey = getObjectKeyToServer(pubType);
                        isErrorImage = false;
                    }
                    String imageurl = "http://"+BUCKET_PUBLISH+"."+endpoint+"/"+objectKey;
                    datas.get(index).setContent(imageurl);
                    index++;

                    if (index >= datas.size()){
                        //说明已经上传结束
                        listener.onsuccess(datas);
                        return;
                    }

                    putSortRichImages(pubType, datas, listener);
                   // 递归同步效果
                }

                @Override
                public void onFailure(PutObjectRequest request, ClientException client, ServiceException service) {
                    // 上传失败
                    // 请求异常
                    if (client != null) {
                        // 本地异常如网络异常等
                        client.printStackTrace();
                        listener.onFailure("本地异常");
                    }
                    if (service != null) {
                        // 服务异常
                        Log.e("ErrorCode", service.getErrorCode());
                        Log.e("RequestId", service.getRequestId());
                        Log.e("HostId", service.getHostId());
                        Log.e("RawMessage", service.getRawMessage());
                        listener.onFailure("errorCode="+service.getErrorCode()+"=rawMessage="+service.getRawMessage());
                    }
                }
            });
        }

        /**
         * 上传用户头像图片/社团logo
         * */
        public OSSAsyncTask putHeadToOss(String path, final HttpCallBack callBack){
            File file = new File(path);
            if (null == file || !file.exists()) {
                callBack.onError(-1, "文件不存在");
                return null;
            }

            OSSAsyncTask task = putAloneImage(1, path, callBack);

//            OSSAsyncTask task = putAsyncSingleProgress(1, path, null, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
//                @Override
//                public void onSuccess(PutObjectRequest request, PutObjectResult result) {
//                    // 上传成功
//                    if (isErrorImage){
//                        objectKey = getObjectKeyToServer(1);
//                        isErrorImage = false;
//                    }
//                    String headUrl = "http://"+BUCKET_HEAD+"."+endpoint+"/"+objectKey;
//                    callBack.onResp(0, "上传成功", headUrl);
//                }
//
//                @Override
//                public void onFailure(PutObjectRequest request, ClientException client, ServiceException service) {
//                    // 上传失败
//                    // 请求异常
//                    if (client != null) {
//                        // 本地异常如网络异常等
//                        client.printStackTrace();
//                        callBack.onError(-1, "本地异常");
//                    }
//                    if (service != null) {
//                        // 服务异常
//                        Log.e("ErrorCode", service.getErrorCode());
//                        Log.e("RequestId", service.getRequestId());
//                        Log.e("HostId", service.getHostId());
//                        Log.e("RawMessage", service.getRawMessage());
//                        callBack.onError(-1, "本地异常");                    }
//                }
//            });
            return task;
        }

    /**
     * 上传单张图片
     * @param type 1头像  2享说 3U城内容,社团纳新,社团/校吧内的贴子内的图片;
     * */
    public OSSAsyncTask putAloneImage(final int type, String path, final HttpCallBack callBack){
        File file = new File(path);
        if (null == file || !file.exists()) {
            callBack.onError(-1, "文件不存在");
            return null;
        }

        OSSAsyncTask task = putAsyncSingleProgress(type, path, null, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                // 上传成功
                if (isErrorImage){
                    objectKey = getObjectKeyToServer(type);
                    isErrorImage = false;
                }
                String path = "";
                switch (type){
                    case 1:
                        path = "http://"+BUCKET_HEAD+"."+endpoint+"/"+objectKey;
                        break;
                    case 2:
                        path = "http://"+BUCKET_SHARE+"."+endpoint+"/"+objectKey;
                        break;
                    case 3:
                        path = "http://"+BUCKET_PUBLISH+"."+endpoint+"/"+objectKey;
                        break;
                }

                callBack.onResp(0, "上传成功", path);
            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException client, ServiceException service) {
                // 上传失败
                // 请求异常
                if (client != null) {
                    // 本地异常如网络异常等
                    client.printStackTrace();
                    callBack.onError(-1, "本地异常");
                }
                if (service != null) {
                    // 服务异常
                    Log.e("ErrorCode", service.getErrorCode());
                    Log.e("RequestId", service.getRequestId());
                    Log.e("HostId", service.getHostId());
                    Log.e("RawMessage", service.getRawMessage());
                    callBack.onError(-1, "本地异常");                    }
            }
        });
        return task;
    }

    /**
     * 上传图片 同步
     * */
    public PutObjectResult putImageSync(String fileUrl) {
        // 构造上传请求
        PutObjectRequest put = new PutObjectRequest(BUCKET_PUBLISH, KEY, fileUrl);
        PutObjectResult putResult = null;

        try {
            putResult = oss.putObject(put);

            Log.d("PutObject", "UploadSuccess");

            Log.d("ETag", putResult.getETag());
            Log.d("RequestId", putResult.getRequestId());
        } catch (ClientException e) {
            // 本地异常如网络异常等
            e.printStackTrace();
        } catch (ServiceException e) {
            // 服务异常
            Log.e("RequestId", e.getRequestId());
            Log.e("ErrorCode", e.getErrorCode());
            Log.e("HostId", e.getHostId());
            Log.e("RawMessage", e.getRawMessage());
        }
        return putResult;
    }
    /**
     * oss 下载图片 异步
     * @param pubType 1头像  2享说 3U城内容,社团纳新,社团/校吧内的贴子内的图片;
     * */
    public OSSAsyncTask downImageAsync(final Context context, final int pubType, final String url){
        if (TextUtils.isEmpty(url)){
            return null;
        }

        String bucketName = "";
        if (pubType >0){
            switch (pubType){
                case 1:
                    bucketName = BUCKET_HEAD;
                    break;
                case 2:
                    bucketName = BUCKET_SHARE;
                    break;
                case 3:
                    bucketName = BUCKET_PUBLISH;
                    break;
            }
        }else {
            if (url.contains(BUCKET_HEAD)){
                bucketName = BUCKET_HEAD;

            }else if (url.contains(BUCKET_SHARE)){
                bucketName = BUCKET_SHARE;

            }else if (url.contains(BUCKET_PUBLISH)){
                bucketName = BUCKET_PUBLISH;
            }
        }



        String objectKey = "";
        String newUrl = "";
        LoadingUtils.getUtils().loading(context);

        if (!url.contains("jpg") && !url.contains("png")){
            int dex = url.indexOf('/', 20);
            int dex2 = url.indexOf('?');

            newUrl = url.substring(0,dex2);

            Log.d("jjf", "图片地址="+url);
            Log.d("jjf", "index="+dex);
            Log.d("jjf", "index2="+dex2);
            Log.d("jjf", "newUrl="+newUrl);
            objectKey = url.substring(dex+1, dex2);
        }else {
            objectKey = url.substring(url.lastIndexOf("/")+1);
            newUrl = url;
        }

        Log.d("jjf", "objectKey="+objectKey);

        // 构造下载文件请求
        GetObjectRequest get = new GetObjectRequest(bucketName, objectKey);

        final String finalNewUrl = newUrl;
        OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
            @Override
            public void onSuccess(GetObjectRequest request, GetObjectResult result) {
                // 请求成功
                Log.d("jjf", "下载成功");


                Log.d("Content-Length", "" + result.getContentLength());
                String path = "";//保存路径
                if (pubType ==1){
                    //下载头像图片
                    path = PathUtils.getUtils().getHeadImagePath(finalNewUrl);
                }else {
                    path = PathUtils.getUtils().getComImagePath(finalNewUrl);
                }

                InputStream inputStream = result.getObjectContent();
                byte[] buffer = new byte[2048];
                int len;
                FileOutputStream ost = null;
                try {

                    ost = new FileOutputStream(path);
                    while ((len = inputStream.read(buffer)) != -1) {
                        // 处理下载的数据
                        ost.write(buffer, 0, len);
                    }

                    ((Activity)context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            UCommonUtil.getUtil().showToast(context, "下载成功");
                        }
                    });
                    //关闭流
                    inputStream.close();
                    ost.close();
                    LoadingUtils.getUtils().hide();

                    //通知图库更新
                    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(path))));
                    Log.d("path", "path==="+path);
                } catch (IOException e) {
                    LoadingUtils.getUtils().hide();
                    ((Activity)context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            UCommonUtil.getUtil().showToast(context, "下载失败");
                        }
                    });
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                UCommonUtil.getUtil().showToast(context, "下载失败");
                LoadingUtils.getUtils().hide();
                Log.d("jjf", "下载失败");

                // 请求异常
                if (clientExcepion != null) {
                    // 本地异常如网络异常等
                    clientExcepion.printStackTrace();
                }
                if (serviceException != null) {
                    // 服务异常
                    Log.e("ErrorCode", serviceException.getErrorCode());
                    Log.e("RequestId", serviceException.getRequestId());
                    Log.e("HostId", serviceException.getHostId());
                    Log.e("RawMessage", serviceException.getRawMessage());
                }
            }
        });

        return task;

    }

    /**
     * 下载图片 同步
     * */
    public void downImageSync(String url){

            // 构造下载文件请求
            GetObjectRequest get = new GetObjectRequest(BUCKET_PUBLISH, url);

            try {
                // 同步执行下载请求，返回结果
                GetObjectResult getResult = oss.getObject(get);

                Log.d("Content-Length", "" + getResult.getContentLength());

                // 获取文件输入流
                InputStream inputStream = getResult.getObjectContent();

                byte[] buffer = new byte[2048];
                int len;

                while ((len = inputStream.read(buffer)) != -1) {
                    // 处理下载的数据，比如图片展示或者写入文件等
                    Log.d("asyncGetObjectSample", "read length: " + len);
                }
                Log.d("asyncGetObjectSample", "download success.");

                // 下载后可以查看文件元信息
                ObjectMetadata metadata = getResult.getMetadata();
                Log.d("ContentType", metadata.getContentType());


            } catch (ClientException e) {
                // 本地异常如网络异常等
                e.printStackTrace();
            } catch (ServiceException e) {
                // 服务异常
                Log.e("RequestId", e.getRequestId());
                Log.e("ErrorCode", e.getErrorCode());
                Log.e("HostId", e.getHostId());
                Log.e("RawMessage", e.getRawMessage());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    /**
     * 创建删除请求
     * @param pubType 1头像  2享说 3U城内容,社团纳新,社团/校吧内的贴子内的图片;
     * @param objectKey 图片的key
     * */
    public void deleteOssFile(int pubType, String objectKey){
        String bucket = null;
        switch (pubType){
            case 1:
                bucket = BUCKET_HEAD;
                break;
            case 2:
                bucket = BUCKET_SHARE;
                break;
            case 3:
                bucket = BUCKET_PUBLISH;
                break;
        }

        DeleteObjectRequest delete = new DeleteObjectRequest(bucket, objectKey);
        // 异步删除
        OSSAsyncTask deleteTask = oss.asyncDeleteObject(delete, new OSSCompletedCallback<DeleteObjectRequest, DeleteObjectResult>() {
            @Override
            public void onSuccess(DeleteObjectRequest request, DeleteObjectResult result) {
                Log.d("asyncCopyAndDelObject", "success!");
            }

            @Override
            public void onFailure(DeleteObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                // 请求异常
                if (clientExcepion != null) {
                    // 本地异常如网络异常等
                    clientExcepion.printStackTrace();
                }
                if (serviceException != null) {
                    // 服务异常
                    Log.e("ErrorCode", serviceException.getErrorCode());
                    Log.e("RequestId", serviceException.getRequestId());
                    Log.e("HostId", serviceException.getHostId());
                    Log.e("RawMessage", serviceException.getRawMessage());
                }
            }

        });

    }


        //1头像  2享说 3U城内容,社团纳新,社团/校吧内的贴子内的图片;
        private String getObjectKey(int type, File file){
            String fileSuffix = "";
            if (file.isFile()) {
                // 获取文件后缀名
                fileSuffix = file.getName().substring(file.getName().lastIndexOf("."));
            }
            String objectKey = "";
            switch (type){
                case 1:
                    objectKey = BUCKET_HEAD+"-"+"and_"+UCommonUtil.getUtil().getId()+"_" + System.currentTimeMillis()+ index + fileSuffix;
                    break;
                case 2:
                    objectKey = BUCKET_SHARE+"-"+"and_"+UCommonUtil.getUtil().getId()+"_" + System.currentTimeMillis()+ index + fileSuffix;
                    break;
                case 3:
                    objectKey =BUCKET_PUBLISH+"-"+"and_"+UCommonUtil.getUtil().getId()+"_" + System.currentTimeMillis()+ index + fileSuffix;
                    break;
            }
            //iOS手机不识别除jpg和png之外的图片  用oss将图片转为jpg的
            if (!fileSuffix.equals(".jpg") && !fileSuffix.equals(".png")){
                isErrorImage = true;
            }
            return objectKey;
        }

    //iOS手机不识别除jpg和png之外的图片 如果不是这两种格式 将图片提交后， 将带上图片处理格式的style，传到我们自己后台 方便ios能展示图片 oss后台还是正常objectKey
    //1头像  2享说 3U城内容,社团纳新,社团/校吧内的贴子内的图片;
    private String getObjectKeyToServer(int type){
        switch (type){
            case 1:
                objectKey = objectKey + "?x-oss-process=style/user_style";
                break;
            case 2:
                objectKey = objectKey + "?x-oss-process=style/share_style";
                break;
            case 3:
                objectKey = objectKey + "?x-oss-process=style/publish_style";
                break;
        }
        return objectKey;
    }




    public interface OSSListener{
            void onProgress(long currentSize, long totalSize);
            void onSuccess(List<String> ossList);
            void onFailure(String message);//异常描述
        }

}
